package main

import (
	"fmt"
	"os"
)

type student struct {
	id   int64
	name string
}

// Student Manager
type studentMgr struct {
	allStudent map[int64]student
}

func (s studentMgr) showStudents() {

	if len(s.allStudent) != 0 {
		for _, stu := range s.allStudent {
			fmt.Printf("Number: %d Name: %s\n", stu.id, stu.name)
		}
	} else {
		fmt.Println("No student stored for now, please add some.")
	}
}

func (s studentMgr) addStudent() {
	var (
		id   int64
		name string
	)

	fmt.Print("Please input student number: ")
	fmt.Scanln(&id)
	fmt.Print("Please input student name: ")
	fmt.Scanln(&name)

	_, ok := s.allStudent[id]

	if !ok {
		newStu := student{
			id:   id,
			name: name,
		}
		s.allStudent[newStu.id] = newStu
	} else {
		fmt.Println("this student has been stored, please check!")
	}
}

func (s studentMgr) editStudent() {
	var id int64
	fmt.Print("Please input student number: ")
	fmt.Scanln(&id)

	_, ok := s.allStudent[id]

	if ok {
		fmt.Print("Please input a new name for this student: ")
		var name string
		fmt.Scanln(&name)

		s.allStudent[id] = student{
			id:   id,
			name: name,
		}
	} else {
		fmt.Println("cannot find this student, please check!")
	}
}

func (s studentMgr) deleteStudent() {
	var id int64
	fmt.Print("Please input student number: ")
	fmt.Scanln(&id)

	_, ok := s.allStudent[id]

	if ok {
		delete(s.allStudent, id)
	} else {
		fmt.Println("cannot find this student, please check!")
	}
}

func showMenu() {
	fmt.Println(`
	--------------------
	1. Show all students
	2. Add a student
	3. Edit a student
	4. Delete a student
	5. Exit
	--------------------
	`)
}

func main() {

	var smr studentMgr
	smr = studentMgr{
		allStudent: make(map[int64]student, 100),
	}
	fmt.Println("----------Welcome SMS!----------")
	for {
		showMenu()

		fmt.Print("Please input a command number: ")
		var choice int
		fmt.Scanln(&choice)
		fmt.Println("You have inputed:", choice)

		switch choice {
		case 1:
			smr.showStudents()
		case 2:
			smr.addStudent()
		case 3:
			smr.editStudent()
		case 4:
			smr.deleteStudent()
		case 5:
			os.Exit(0)
		default:
			fmt.Println("unrecognized option!")
		}
	}
}
